//! # Minimal User Decryption
//!
//! Decrypt an encrypted value using KMS client api.
//! This example is taken from a centralized KMS configuration.
//!
//! ```bash
//! cargo run --example minimal-user-decryption-response
//! ```

use alloy::primitives::U256;
use fhevm_gateway_bindings::decryption::Decryption::CtHandleContractPair;
use gateway_sdk::{FhevmSdkBuilder, Result, utils::validate_address_from_str};
use std::str::FromStr;
fn main() -> Result<()> {
    println!("🔓 Processing user decryption...");

    let sdk = FhevmSdkBuilder::new()
        .with_gateway_chain_id(8006)
        .with_host_chain_id(11155111) // Example: Ethereum Sepolia
        .with_decryption_contract("0x1234567890123456789012345678901234567bbb")
        .with_input_verification_contract("0x66f9664f97F2b50F62D13eA064982f936dE76657")
        .with_acl_contract("0x0987654321098765432109876543210987654321")
        .build()?;

    let handle_pair = CtHandleContractPair {
        ctHandle: U256::from_str(
            "0x0000000000000000000000000000000000000000000000000000000000000000",
        )?
        .into(),
        contractAddress: validate_address_from_str("0x526D4FaC5534A00Abc5EF1c1eCC71568A8Db716A")?,
    };

    let json_response = r#"{
        "response": [{
            "payload": "2900000000000000210000000000000002cab63837bc849b9ca44cfa78d2b3e41ac835845fd6d2a191faa2f0e6a975b77120000000000000001b6f645998fbfc4f54c00ff68897f00bc829226dd53addcac270d3fc79fe0325010000000000000002000000e1060000000000004fb9c6c2dbfe2262f0457cce20060000000000003fc1dd711b822f4a78a3e4a76f16b1cb9dd205a6b8bf0a1b61b4aa7f844825d800a592666b3a6113eeca4c17171ef5d53fdf97f75c713553ac1770ec94f6629dbd15efc2b4ef1b12f4035408c7aee36d989a5a94df5e21914437512ef32efa5eef1bb6c14985d6e89194d8b1eb603b968ff7bd7af3ace246a29c2743c1f851a89595f639f4c04fb5e337736018cbfa1b3430da02299d152ff69449b1703daea71151ba9cf890505eaed535b6ad5424e6cae89d74365f8faab511ac3b262d610a45e031aeda607b61ed7a3fe1bb734c8ac53e2c305b2b83142830be4e33eef212479af18e655d068def3bb63e2d7e0c87e102c76fa92e906a07f64a597847f25fcea52cefb5ade1236cf94feac284e5290254422b5ce0bc51140c92a7fafcf67a36316e842641de64611665a50b536b59ef3a3e12f358a1990e3da166ca0eb8f0be3e1ff92a00e1989af50ee9619675c8f300bf1ee434c7573a1b18157e9c9f2de1e6a84ce0bded220f74f5721892e0781e85399ab181bd1f3e45d0b726223c7f0e9b7f979b87548738b5470e85ff85961ed4b3767913cd41905abdc7884b7228534b7c4e8b3af65519cf2a0dae6e89919cceae2bcf3620b223bb7f2a38d09549e25936768d737fd2b9cf6a757c7dd27827a8ec87904115ed3106f29b0413faa252d4ceb64aa38950a07c927d506333414ff88cdcb0b2bcb733e1f16eea5b86687d1fef3bd9735789a646974c5a4cedfe2722a334462e423568cea2c4bf9322352bf96436047aec504824973cbea175d0553814910233b14f0f5d89381d0ab7a1fd6b378e79d7ed678213e27f72b3c51622badb267f300b20a2385316a3a03f90c5188f3a26efdeb6573179a52cb60864f8b1600404230ac558e99cbb23dfc98d1a32f204cc8de1d18169561139098b800b9028e5ae1e83a61eee4ccd3b4e3fb1d4fc51b26ae58452526f0d1a61d9587deaecd26825f8ebc75aad08d571271b6daeb9f6cd56878f93bcb9507d145c13e56ff3d73c7446c5c281880683d5d737993b7a1827658dcc827d6baf3be5d9e97710986d21ea920982d38525f6a6eb0d648189df7887c49e9f91e0a315b89767f0d10480342a53026d2c101d36535b4e4e48df2d3438783b00e3d4f61b38bb0b15e709c7550fdb7ded1a984c9edd780fa7ffb2ed35cc262417c883889f115e42de39b5e77f10f67a7c93546d2aa824f74c5b1ede6289db9ff13d717379284df380c129cdb106b2e5fb82ecafd649f50b4497686f84f74258695a7afb4f26c0242f907f12f4ebb3cfae2354c0ef37b0b66067bcf7a88c3cb7ed163abf8a8dd6fc4bcf73f79b6071fec4965f5b97ccabd8da7c3b2f7eebd2687c0b29c24bcc5cdd0aaa2f95da7ccc57a4c8d88c58eccc0019b835872d04a435a091a3c9a05816c56fe44d26d84e943a46637aa06c37eeb58503a71eb9a10c00b8766ece3729ec343f4919665f12ca1b7de72ea0ea95014c46a2a238f91c20b620fb6899dfd9a73420cb4f672b83f52f6aaa73b4f97b39977fb3ff8f904757eb830e1e5d4e961ebf36be05a581f2fbd69e24a603571316ec1e841289dbdeb834c0f98e3db7c75ec5197b5e3e2f357ea84c967d048876bc622ab1e7717d263c8c73a933bd026aed587d7c8d8bd2053ef9a10c24111493dddd26bb465c893d7d584f7d2ba55453806744327719bb4ccf81e201c8144a3966cba1a63ec1f26e1533b110bfbb732e35e95184c1efd089f00f79d1838ca938b1efe20951b03c0710c6f6adae77bba2bef4aa3afe2b18870fb609f1cc531da1f95529fd8e9b11e20eab00f28a9b5bd8c3979c37ce749cd906521fe58cfa4abcd8142aaf84314ed6acd3b74c0215e596d17f3d17af29b423c0cfed2b50901655ea955e19962f734468ae77bd2e9bfd502834c0cc96a589105cec4b529b106ac4617d587ef8d3a278486439a21bf97d8fbb452c15a728511e1fa759594fc55c34ffe9da7e28a695893c356aa29541985e32f97ff75d7e79cd014aceeb54a15e44863fda8c57db5c71e5ff17c10362f8882d572140b6221f3b4f09671e9d36133f5b4cb7bca057a64c03fd6032d47a155492cb23111810f186d74b496630b66d15859c7e80eabd958e694f7f3a872f606f77ed9362f3de1ae1eb99df2ad6190ca61f4df8b98d9e0a57f96304f5cd7ddefdf0837eda2f4cd7223bef49e84c995ea89d831ca50000000000000027ac685e3084f168b426251f39410e17c993b86fab3bfa4357adb4de61352414cee54fb10968cd19fd3dfa79622bcffb05c39ea6702220f0bbc9a59910b8bfa333ce832e54fccbcdf02b02ad55e62596cb6a78925bd3a39773b2cd7f1dd58ba5dfa9327beaa5976e598c8ffd78af3f276e63f54e566076d2be31f034b307e0604bd3814dacb3d7400bb55b0f4a8d18c610bc9b51495d18e56200ab9496dcc38d8a5380809e08000000000000000000000000000000010000000100000000000000",
            "signature": "ddd514aa094b8a9aff0314a749eef12e2d1d36c44484f1814ae1be9aeb8a9eef41e87d2bde2019131f81a25dcc482bcc0d1ed3cc9f65ebda0a44b9fc42f091621b"
        }]
    }"#;

    let result = sdk.create_user_decrypt_response_builder()
        .with_kms_signer("0x0b7cd831624B5D506dd818acb3b3b8FDb813Bb4D".to_string())
        .with_gateway_chain_id(8006)
        .with_user_address("0x17853A630aAe15AED549B2B874de08B73C0F59c5")
        .with_verifying_contract_address("0x66f9664f97F2b50F62D13eA064982f936dE76657")
        .with_public_key(
            "20060000000000006e1258e7072f662714fea86e0b084c0fe6bb2cac7021981a4e721cefca37e8631410a27ee93b433da26d4dac058376a57de9ba0b0123b9dabafd9332cda826a96c34a44561346c1fa2c3c9a69231a90a62d8e27bd6139b1fb5af9752c4c190757c9a1737a97961ec4073f88414a04947f19029d3cbbe945deb6625ccf6aeeaa625d6555386a6ac72899afd69c394267f8bc8933304640d988a3cc8808ad5bcbcc6b8735b1d5e6bc34d05682f992c1a6466a2919eff9708b8882b5548399a0bd07ba51093411736869e4e9b165929b813a94718dab0f87922ff6917e7c067d5d0072912c6b63638bb5c3da1b173392a829d85c2d11348ebf8a16f5bc3405b407eb95d0e7a5271d59e40c6cb10006288abb73e9b2b85556dabd54c45f139bb055b5764a1259cb06e740a6848a1f4018fec6371cf3c78352103280810ec693d0045a61776cbdd574b93e98e67d996fb800c9be59a6d920a367b8280a49b518399af254b9a1c03369cbb5e0bb69d014b77f9528d9c60483aa2bd4baf1117b4c4a441a58a28f9941389628d394674b4520fec91a7ccba237482c1bf375b3e88ac5d27b1fc92890ab0af05075e0bb907368565014551dee28add4598b9c9739fbc90aed869c417213091b30b309274123789a43dbd21b8b2a42b11490e305633f9e958677917d3398c95e47f9ed878a1313543d999a42c746aeb477db5323ca6220348b57a589f84441cb56a5aec0282de68708835c8746804c43c769c0bc6a14c96a414117c1102218134c1f611ebf7a970b578c12a92847943dfd2c4e993ac23f3c69c123dbc9645af15821911b9238cb86ad995b1e80132bab4bab8420a87853cf253a415c114fa3cbaf89571e60583b3a99f5894ccb411eb3ba515859f34599533d28b06a3494f00c68093c859296bcb28ce76a320211c0385081a205327d1c385b52b6e067989e6c814c5bc20d595ab70699a5714c728cc1cdcc0c2ef9441a582661fa187a45cc589385095b29d4dca0008f07b3ee7ceb3a5895ddb43d6631a241717eb3981349393da522395baca70e0934c2278c84704b85a10d0e56d43562067d50c6369b5e54b509f896f26bb4c92e34858604e70e2436ae17aba6371eaf74dfdd991ea9b9b2212b5fb93b3296512f533795891b72e57c0beb89f5b764edd172b6243408a8a3f5d89211e8c2086d2372c5a138c91860044cee4ba099b00cfd46c38013763713906cb5b00b45777e849a018631e2ee6a8f721982d88b66317b2ae1639846c571305b501e8ac5bb01c051b9bb6730f168679fdd582e23a1ff396185fac58ffd6ba755c603ee5a030ca49d1ba278216c61c483b84058c37b79b5ebc2df8725e7958502af7590be602d345c831ea0d3f855af95a1907b09ec354bc856865b045c6f09c88003d86816b5777504d090a45515ba95bf88c502a7a0f906d4bb468fb26278f7483d5e59fbb719012ca32f54a1cc90b22fd751d25d06c6850c0c68340113c1395166474062485b1c0afbbbe48a72b4f2bbae22c3686803fced1300452c9ba282716d44e7a549b51d5680ce0a275929e3d3429e3616d29e91f7e03cd8f7a85f42509a6baaf462164646bbd662252cc3375bc83c74979a5aee063eff67a3c291ed4674f6c216c2c2713e10002b5f67f211a89eef175df2a38086204dd6b408cf53f2a712e3e943cf7ca26c3c28163602bbea5c2071670884ba91bbc646a517c2d754d7bdccc9e351c4f928e281b6896b588fe0b4558cc3fa9e81c86c5016a54ca463561283867c82080f9f79175f04c19479188fc28f0f58cd7132669dc0db46721771ba646781df6486ba0da86c9031924db278eb94d9f6a3704628cdf6c33f046b7bf35cba2373e5f43420c817fbdfc7fc273638f070f5df70314553ad64859fcfa389c01521aa05e5e046800616cf8051140189311080d4dcc9d2afb9db029287d1cba3fcc6a3f855629183bdafb2ccb488c00170909dc94d124580a0875a3da72c6d38f17b68869a660983b488243a5f8a9682203a9d903ab14aaa600bbb410491dd25a44af0ac6d5f904eea4b49b703fe4797574f31310f21d6c18a9abdc0d86e603528a16dd816ab1f6450f41cb567a1762921739d8cae630b7fa0946a6104d393912c267aaaae5ad87e47973f7a0413a52f1e51dd04d08efeb94929875d177e1afb14b718ba75f3c3605c86b1ef0e7701f711b2b",
        )
        .with_private_key(
            "600c000000000000fcdc7478c6307d73b4a1244ce06c98988aa7095412041795dd404eec043282dc67ee155b731a0adc7a44e3185f20bc67cac1354e7584d4d9842a5bab4908443a8b1946812da212a12fa51ddc6993d46b177752c50c041080508baef6c58ffa73bd119b5db4ccb3227443d003ffd8a337511084d0a936c4720b2a37e6f56339f9cb2b6072b4c72ed03ca95e633c68bbcdecac9ad1613fc5802727352f5079a9860909b885ce378b54b1f84b03a7291820a12ed79e2d83375e0647b894c7f593318ba94873265aedab74b17896dd774eb2b7cf34c91bac24bb50da497aa1570d86105d7316c011008bc25cfa6a2378a0a20b144ce3e10cfd2970b377c2b6d218a94827bb95124212866956180c72b5f35b5dc467230122a3e447c553db3a9efa278929a9e2f4b26f23293d918c9ff8218d18cb4a1a50e930ad8cda150681510242742a6c089b354bef376c7cfcb929f17c3a4c7da442af54e38659a8c566630db10a0f0905b1d9c1167e014663cb476e0704d3d4422f592ef5e94fdd181cbdec177115a145ba6418316ed54323aa8bb29d560652e856a85204a186bac089478ef95070088b3df8a259c939cdd6c7dad16e31eac69b843bf7e73253689bcbb411910177ad01cc1aa590ef104af8f357297b85f4d09b99e71aaf108bc4664935b82424f584bdd4b22ec603e0eba6acc2189f647799b22b0729b06af12a25f94d54d02718b28e6ae65ccb4ab3931c94e2e2063833cd668b2913845372e5a04ccaa8afdab3f449a56cfc8a7587aa51524859876051126ecca04e079cb022c30b9da2090a26685e6521b112877b66275a24018f88335d952e86237a6a28b6803a7b19590f1e32c8e10640c66abd72224822b2c4f3aa8e848c4114f95231386ffe2a33deac278f486e62e24629385aec2a9a4a774a274b473d88a4dd2c5a214170761b498f758ca1e1046f371b1cf051f5885eed9a271548af44051b04e55156b99a0fa2cacde1942ab77555149ed0519ca8fcbfe6569422e70876e72a9ad45933408b0f450a56c47005070482a61ce3f363c2b852c6a1935b6b0587305b8dca3112568bc66c3354bac3af438c47fc83b3f13eec40bfbb3c5ebfd0a9e98825c5f3bcf7d08e7ce6cc9fb8bd58f6cd0e3256821053df8235e3432fdbda051a02b34c14504e60b86c2597b73905690889d8614595ca6db38b20c1a6030ec34dc88990cbc4505b822fa5a39be19a60d2abb2121c116ec6236916c3c525053f43636a87a440d26bd83c240184a877b81cfe9881cc583740f2a263f5bc187b38dd155acbf099797a826d8a6eef022489cb998194a7075017045a4c856b8e2f5915854188e7cace7b0358e8976eaa2c40dfb2583bc473c759504c879706a4822cb0a9fe7c53dd0a127c6b5f5e136659d6488128ae7ac37a3a7c8d9191c75d44731e46b71350748493762287cf82345942bab3c6c1169723c58c762f95f43eb5e42fa45a1040a1241d601c52a2212b596e4307b3cc54a4dcd98a49376fcd4c0c1810b745069180635bb62bb1cf1886ff0a318c4196d5fc72c3081a91a351642866372b042624a3e9fac823bc4ab810adac62546094077d222641f7a7a8797d82c3b7c78597fe88a7b26219ba5299370b271da20c34ba05b18b3fb72576ebe29e7a0b79907946c866040ec374d9509977200859c09f91f99e7e8c6e8b6c97ccac6dda02b9029ca13cdab23ed8ac37c2964f40a0ef071a3755572e04c93476b5109bca1944cf5cb4593c174234c7a121e50a0f13b3ae2b1bc7c62bc7e491ba203c30eb14175b542c4a37fc5c6e134212b130354a58c9fdd81fd9044312f8bf61d1c598a9940292411e1a95a5d2392434815087378ef98a73a59af1ea8dc045bd65e53f5e42737ec9c091e1a680c0757f393c5da8974fcbb934a4729182197ec9a90b083dbee755ff20824d47738a7835351b4f11d99bfb96a148a7ac62270741588ea672ac91f78c81c828c4610129d9052bf38eca1b1a89f23b866a2453ba96a94ba8d69245d5a29dab64b4c5098da7516354a73763b7cfbb4a096df73528a68a589c4eaca8b575d09974abcc565bc6aed33990b200274a7901808f1d50c0875b42788cae010273fa8bb1147a2ff8eba4b9e8a8e98c4ab0e0b9f7585d388b8dd22959e398b14b8c3a6e1258e7072f662714fea86e0b084c0fe6bb2cac7021981a4e721cefca37e8631410a27ee93b433da26d4dac058376a57de9ba0b0123b9dabafd9332cda826a96c34a44561346c1fa2c3c9a69231a90a62d8e27bd6139b1fb5af9752c4c190757c9a1737a97961ec4073f88414a04947f19029d3cbbe945deb6625ccf6aeeaa625d6555386a6ac72899afd69c394267f8bc8933304640d988a3cc8808ad5bcbcc6b8735b1d5e6bc34d05682f992c1a6466a2919eff9708b8882b5548399a0bd07ba51093411736869e4e9b165929b813a94718dab0f87922ff6917e7c067d5d0072912c6b63638bb5c3da1b173392a829d85c2d11348ebf8a16f5bc3405b407eb95d0e7a5271d59e40c6cb10006288abb73e9b2b85556dabd54c45f139bb055b5764a1259cb06e740a6848a1f4018fec6371cf3c78352103280810ec693d0045a61776cbdd574b93e98e67d996fb800c9be59a6d920a367b8280a49b518399af254b9a1c03369cbb5e0bb69d014b77f9528d9c60483aa2bd4baf1117b4c4a441a58a28f9941389628d394674b4520fec91a7ccba237482c1bf375b3e88ac5d27b1fc92890ab0af05075e0bb907368565014551dee28add4598b9c9739fbc90aed869c417213091b30b309274123789a43dbd21b8b2a42b11490e305633f9e958677917d3398c95e47f9ed878a1313543d999a42c746aeb477db5323ca6220348b57a589f84441cb56a5aec0282de68708835c8746804c43c769c0bc6a14c96a414117c1102218134c1f611ebf7a970b578c12a92847943dfd2c4e993ac23f3c69c123dbc9645af15821911b9238cb86ad995b1e80132bab4bab8420a87853cf253a415c114fa3cbaf89571e60583b3a99f5894ccb411eb3ba515859f34599533d28b06a3494f00c68093c859296bcb28ce76a320211c0385081a205327d1c385b52b6e067989e6c814c5bc20d595ab70699a5714c728cc1cdcc0c2ef9441a582661fa187a45cc589385095b29d4dca0008f07b3ee7ceb3a5895ddb43d6631a241717eb3981349393da522395baca70e0934c2278c84704b85a10d0e56d43562067d50c6369b5e54b509f896f26bb4c92e34858604e70e2436ae17aba6371eaf74dfdd991ea9b9b2212b5fb93b3296512f533795891b72e57c0beb89f5b764edd172b6243408a8a3f5d89211e8c2086d2372c5a138c91860044cee4ba099b00cfd46c38013763713906cb5b00b45777e849a018631e2ee6a8f721982d88b66317b2ae1639846c571305b501e8ac5bb01c051b9bb6730f168679fdd582e23a1ff396185fac58ffd6ba755c603ee5a030ca49d1ba278216c61c483b84058c37b79b5ebc2df8725e7958502af7590be602d345c831ea0d3f855af95a1907b09ec354bc856865b045c6f09c88003d86816b5777504d090a45515ba95bf88c502a7a0f906d4bb468fb26278f7483d5e59fbb719012ca32f54a1cc90b22fd751d25d06c6850c0c68340113c1395166474062485b1c0afbbbe48a72b4f2bbae22c3686803fced1300452c9ba282716d44e7a549b51d5680ce0a275929e3d3429e3616d29e91f7e03cd8f7a85f42509a6baaf462164646bbd662252cc3375bc83c74979a5aee063eff67a3c291ed4674f6c216c2c2713e10002b5f67f211a89eef175df2a38086204dd6b408cf53f2a712e3e943cf7ca26c3c28163602bbea5c2071670884ba91bbc646a517c2d754d7bdccc9e351c4f928e281b6896b588fe0b4558cc3fa9e81c86c5016a54ca463561283867c82080f9f79175f04c19479188fc28f0f58cd7132669dc0db46721771ba646781df6486ba0da86c9031924db278eb94d9f6a3704628cdf6c33f046b7bf35cba2373e5f43420c817fbdfc7fc273638f070f5df70314553ad64859fcfa389c01521aa05e5e046800616cf8051140189311080d4dcc9d2afb9db029287d1cba3fcc6a3f855629183bdafb2ccb488c00170909dc94d124580a0875a3da72c6d38f17b68869a660983b488243a5f8a9682203a9d903ab14aaa600bbb410491dd25a44af0ac6d5f904eea4b49b703fe4797574f31310f21d6c18a9abdc0d86e603528a16dd816ab1f6450f41cb567a1762921739d8cae630b7fa0946a6104d393912c267aaaae5ad87e47973f7a0413a52f1e51dd04d08efeb94929875d177e1afb14b718ba75f3c3605c86b1ef0e7701f711b2b990f78a6ac3afaf0fa2259d9e77c7a5dacc726581305c34ce43c89ad783c92f33b7ebcbfd2a5f2e1e0233e7128a256e2217fa0509724b25829afcd941a07ed01",
        )
        .with_handle_contract_pairs([handle_pair].to_vec())
        .with_signature("eed514aa094b8a9aff0314a749eef12e2d1d36c44484f1814ae1be9aeb8a9eef41e87d2bde2019131f81a25dcc482bcc0d1ed3cc9f65ebda0a44b9fc42f091621b")
        .with_json_response(json_response)
        .with_verification(true)
        .with_domain("Authorization token")// Be carefull, domain should be Decryption, but this example has been generated with Authorization token
        .process();

    match result {
        Ok(res) => println!("✅ Decryption completed! (Expected result: 48); {res:?}"),
        Err(e) => println!("⚠️ Decryption failed (expected in test env): {e}"),
    }

    Ok(())
}
